// api.js
import axios, { type InternalAxiosRequestConfig } from 'axios';
import { ElMessage } from 'element-plus';
const API_BASE_URL = 'https://gzzyk.yl1001.com/gzzyk/front/api/'; // 根据实际情况设置基础URL
const apiClient = axios.create({
  baseURL: API_BASE_URL,
  // 其他axios配置选项
});
// 请求拦截器
apiClient.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    const isToken = config.headers?.isToken === false;
    if (localStorage.getItem('token') && !isToken) {
      config.headers['Authorization'] = 'Bearer ' + localStorage.getItem('token'); // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    
    // FormData数据去请求头Content-Type
    if (config.data instanceof FormData) {
      delete config.headers['Content-Type'];
    }
    return config;
  },
  (error: any) => {
    return Promise.reject(error);
  }
);
// 封装具体的接口请求函数
const api = {

  async getPosts() {
    try {
      const response = await apiClient.get('/api/user/register');
      return response.data;
    } catch (error) {
      ElMessage.error('123');
      throw error;
    }
  },

  //注册
  async createPost(postData: registerType) {
    try {
      const response = await apiClient.post('/api/user/register', postData);
      return response.data;
    } catch (error) {
      console.error('', error);
      throw error;
    }
  },

  //登录
  async createLogin(postData: loginType) {
    try {
      const response = await apiClient.post('/api/user/userLogin', postData);
      return response.data;
    } catch (error) {
      console.error('', error);
      throw error;
    }
  },

  //退出
  async logout() {
    try {
      const response = await apiClient.post('/logout');
      return response.data;
    } catch (error) {
      console.error('', error);
      throw error;
    }
  },

  // 文章查询
  async articleList(getData: any) {
    console.log(getData)
    try {
      const response = await apiClient.get('/course/getMajorCourse', { params: getData });
      return response.data;
    } catch (error) {
      console.error('', error);
      throw error;
    }
  },

    // 首页数据
    async dashbordList(getData: any) {
      console.log(getData)
      try {
        const response = await apiClient.get('/home/dashbord', { params: getData });
        return response.data;
      } catch (error) {
        console.error('', error);
        throw error;
      }
    },

  // 获取文章详情
  async articleDetail(getData: unknown) {
    try {
      const response = await apiClient.get('/api/article/getById', { params: getData });
      return response.data;
    } catch (error) {
      console.error('', error);
      throw error;
    }
  },

    // 获取文章详情
    async articleAbout(getData: unknown) {
      try {
        const response = await apiClient.get('/api/agree/getByType', { params: getData });
        return response.data;
      } catch (error) {
        console.error('', error);
        throw error;
      }
    },

  //发布文章
  async articleAdd(postData: articleAddType) {
    try {
      const response = await apiClient.post('/app/article/add', postData);
      return response.data;
    } catch (error) {
      console.error('', error);
      throw error;
    }
  },

  //发布文章
  async imgUpload(postData: articleAddType) {
    try {
      const response = await apiClient.post('/file/imgUpload', postData);
      return response.data;
    } catch (error) {
      console.error('', error);
      throw error;
    }
  },


async imageUrlToFile(fileName:string) {
  console.log("111",fileName)
  try {
    const response = await apiClient.post('/file/imgUpload', { imgFile: fileName })
    const blob = new Blob([response.data], {
      type: response.headers['content-type']
    })
    const file = new File([blob], fileName, {
      type: response.headers['content-type']
    })
    return file
  } catch (error) {
    console.error('Error fetching the image:', error)
    return null
  }
}
  // 可以继续添加其他接口请求函数
};

export default api;